Events = function() {
}
Events.connect = function(target, eventType, handler) {
    if (target.attachEvent) {
        target.attachEvent("on" + eventType, function() {
            handler(window.event);
        });
    } else {
        target.addEventListener(eventType, handler);
    }
}

Notifier = function(resourcePrefix) {
    this.resourcePrefix = resourcePrefix;
    this.notifyObj = window.webkitNotifications;
    if (this.notifyObj) {
        this.notifyAllowed = true;
    } else {
        this.notifyAllowed = false;
    }
    this.audioElem = document.createElement("audio");
    if (this.audioElem.play) {
        document.body.appendChild(this.audioElem);
        var source = document.createElement("source")
        source.src = this.resourcePrefix + "notify.mp3";
        source.type = "audio/mpeg";
        this.audioElem.appendChild(source);
        source = document.createElement("source")
        source.src = this.resourcePrefix + "notify.ogg";
        source.type = "audio/ogg";
        this.audioElem.appendChild(source);
        this.audioElem.load();
        source = document.createElement("source")
        source.src = this.resourcePrefix + "notify.wav";
        source.type = "audio/wav";
        this.audioElem.appendChild(source);
    } else {
        this.audioElem = null;
    }
    this.active = true;
    var self = this;
    Events.connect(window, 'focus', function() {
        self.active = true;
    });
    Events.connect(window, 'blur', function() {
        self.active = false;
    });
    this.active = true;
}
Notifier.addNotificationChecker = function() {
    if (window.webkitNotifications === undefined) {
        return;
    }
    var notifier = window.webkitNotifications;
    if (notifier.checkPermission() == 0) {
        return;
    }
    var requestElem = document.createElement("div");
    requestElem.className = "notificationRequest";
    document.body.appendChild(requestElem);
    requestElem.appendChild(document.createTextNode(
            "Ваш браузер поддерживает уведомления. Вы хотите получать уведомления от " +
            "игр?"));
    var buttonsElem = document.createElement("div");
    buttonsElem.className = "notificationButtons";
    requestElem.appendChild(buttonsElem);

    var acceptButton = document.createElement("button");
    buttonsElem.appendChild(acceptButton);
    acceptButton.textContent = "Да";
    acceptButton.onclick = function() {
        notifier.requestPermission();
        requestElem.style.display = "none";
    }
    var rejectButton = document.createElement("button");
    buttonsElem.appendChild(rejectButton);
    rejectButton.textContent = "Нет";
    rejectButton.onclick = function() {
        requestElem.style.display = "none";
    }
}
Notifier.prototype.notify = function(title, text) {
    if (this.active) {
        return;
    }
    if (this.notifyObj && this.notifyAllowed) {
        if (this.notifyObj.checkPermission() == 0) {
            this.notifyObj.createNotification("", title, text).show();
            return;
        }
    }
    if (this.audioElem) {
        this.audioElem.play();
    }
}

Util = function() {
}
Util.setText = function(element, text) {
    if ('textContent' in element) {
        element.textContent = text;
    } else if ('innerText' in element) {
        element.innerText = text;
    }
}